using Microsoft.AspNetCore.Mvc;
using UserManagement.Services;

namespace UserManagement.Controllers;

[ApiController]
[Route("api/[controller]")]
public class FilesController : ControllerBase
{
    private readonly IFileService _fileService;
    public FilesController(IFileService fileService) => _fileService = fileService;

    [HttpPost("upload")]
    public async Task<IActionResult> UploadFile(IFormFile file)
    {
        if (file == null || file.Length == 0) return BadRequest("No file uploaded.");
        var fileRecord = await _fileService.UploadFileAsync(file);
        return Ok(fileRecord);
    }

    [HttpGet("download/{id}")]
    public async Task<IActionResult> DownloadFile(int id)
    {
        var fileResult = await _fileService.DownloadFileAsync(id);
        if (fileResult == null) return NotFound();
        return fileResult;
    }
    
}